/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
package info.decamps.m2settings.model;

import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

public class MirrorTableModel  extends ExtendableTableModel {
    
    private static final long serialVersionUID = -57551943605459164L;
    
    public static final int COLUMN_ID = 0;
    
    public static final int COLUMN_NAME = 1;
    
    public static final int COLUMN_URL = 2;
    
    public static final int COLUMN_MIRROROF = 3;
    
    private Logger log = Logger.getLogger(MirrorTableModel.class);
    
    private Mirrors mirrors;
    
    public MirrorTableModel(Settings userSettings) {
        log.debug("Create MirrorsTableModel with settings: "
                + userSettings.toString());
        mirrors = userSettings.getMirrors();
        if (mirrors == null) {
            mirrors = new Mirrors();
        }
        columnTitles = new String[4];
        columnTitles[COLUMN_ID] = "id";
        columnTitles[COLUMN_NAME] = "name";
        columnTitles[COLUMN_URL] = "URL";
        columnTitles[COLUMN_MIRROROF] = "mirror of";
    }
    
    public Class<?> getColumnClass(int index) {
        return String.class;
    }
    
    
    public int getRowCount() {
        return mirrors.getMirrorCount();
    }
    
    public Object getValueAt(int rowIndex, int columnIndex) {
        Mirror row = mirrors.getMirror(rowIndex);
        switch (columnIndex) {
            case COLUMN_ID:
                return row.getId();
            case COLUMN_MIRROROF:
                return row.getMirrorOf();
            case COLUMN_NAME:
                return row.getName();
            case COLUMN_URL:
                return row.getUrl();
        }
        return null;
    }
    
    public void setValueAt(Object value, int row, int col) {
        Mirror mirror = mirrors.getMirror(row);
        assert (String.class == value.getClass());
        switch (col) {
            case COLUMN_ID:
                mirror.setId((String) value);
                break;
            case COLUMN_MIRROROF:
                assert (String.class == value.getClass());
                mirror.setMirrorOf((String) value);
                break;
            case COLUMN_NAME:
                mirror.setName((String) value);
                break;
            case COLUMN_URL:
                mirror.setUrl((String) value);
                break;
        }
    }
    
    public void addRow() {
        Mirror vMirror = new Mirror();
        addRow(vMirror);
    }
    
    public void addRow(Mirror vMirror) {
        mirrors.addMirror(vMirror);
        fireTableChanged(new TableModelEvent(this, getRowCount(),
                getRowCount(), TableModelEvent.ALL_COLUMNS,
                TableModelEvent.INSERT));
    }
    
    public void removeRows(int[] rowIndexes) {
        for (int i = rowIndexes.length - 1; i >= 0; i--) {
            Mirror vMirror = mirrors.getMirror(rowIndexes[i]);
            mirrors.removeMirror(vMirror);
            fireTableChanged(new TableModelEvent(this, i, i,
                    TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
        }
    }
    
    public String getElementName() {
        return "mirror";
    }
    
    public Mirror getRow(int index) {
        return mirrors.getMirror(index);
    }

    public Mirrors getMirrors() {
        return mirrors;
    }
}
